import { isDef, merge } from './src/utils'

let _Vue
let _options = {
    // 这里时配置项
    route: {
        name: 'VueComponentView',
        path: '/vue-component-view',
        component: null
    },
    content: [],
    view: null,
}
// 添加路由
let routerAdded = false
function addRoutes (router) {
    if(routerAdded) {
        return
    }
    router.onReady(()=> {
        _options.route.component = _options.view
        // 兼容旧版的
        if(router.addRoute) {
            router.addRoute(_options.route)
        } else {
            router.addRoutes([_options.route])
        } 
        
        routerAdded = true
    })
}
// 判断是否是生产环境
const isProd = process.env.NODE_ENV === 'production'
// 安装
export default function install (Vue, options) {
    if (install.installed && _Vue === Vue) return
    install.installed = true
    _Vue = Vue
    
    _options = merge(options, _options)
    
    _Vue.mixin({
        created:function () {
            if(isProd) {
                return
            }
            if(isDef(this.$vcv)) {
                return
            } else {
                this.$vcv = _options
            }
        },
        mounted: function () {
            if(isProd) {
                return
            }
            if(routerAdded) {
                return
            }
            // 挂载路由
            if(this.$router) {
                addRoutes(this.$router)
            } else {
                console.log('没有 router')
            }
        }
    })
}